package com.pcs.controller;

import com.pcs.pojo.Massage;
import com.pcs.pojo.Person;
import com.pcs.pojo.User;
import com.pcs.pojo.UserVerification;
import com.pcs.service.IMassageService;
import com.pcs.service.IPersonService;
import com.pcs.service.IUserService;
import com.pcs.service.IUserVerificationService;
import com.pcs.utils.JWTUtil;
import com.pcs.utils.MD5Encryption;
import com.pcs.utils.ResponseData;
import com.pcs.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/massage")
public class MassageController {
    @Autowired
    IMassageService massageService;
    @Resource
    private IUserVerificationService userVerificationService;
    @Resource
    private IUserService userService;
    @Resource
    private IPersonService personService;

    @RequestMapping(value = "/login.do", method = { RequestMethod.POST })
    public @ResponseBody
    ResponseData login(@RequestBody UserVerification userVer) {
        System.out.println("进入请求接口");
        Massage massage = new Massage();
        massage.setPhone(userVer.getLoginToken());
        massage.setCode(userVer.getPasswordToken());
        System.out.println(massage.getPhone());
        UserVerification userVerification = null;
        Massage test = massageService.SelectByPhoneAndCode(massage.getPhone(),massage.getCode());
        System.out.println("查询结果："+(test!=null));
        if (massageService.SelectByPhoneAndCode(massage.getPhone(),massage.getCode())!=null){
            System.out.println("登录成功，准备获得登录信息");
            userVerification = userVerificationService.selectByPhone(massage.getPhone());
        }
        userVerification = this.userVerificationService.login(userVerification);
        System.out.println("token:" + userVerification);
        ResponseData responseData = ResponseData.ok();
        if (userVerification != null) {
            // 生成token
            String token = JWTUtil.generToken("1", "Jersey-Security-Basic", userVerification.getLoginToken());
            // 向浏览器返回token，客户端受到此token后存入cookie中，或者h5的本地存储中
            responseData.putDataValue("token", token);
            System.out.println("用户id"+userVerification.getuId()+"登录校验成功");
            // 以及用户信息
            User user = this.userService.selectByPrimaryKey(userVerification.getuId());
            try {
                System.out.println("用户登录:"+user.getuName());
            }catch (NullPointerException e){
                System.out.println("登录成功，但是出现异常，未找到登录的用户");
            }

            responseData.putDataValue("user", user);

            // 师生信息
            Person person = this.personService.selectByuId(userVerification.getuId());
            responseData.putDataValue("person", person);

            // 以及用户账号密码信息
            responseData.putDataValue("userVerification", userVerification);
        } else {
            // 用户或者密码错误
            responseData = ResponseData.customerError();
        }
        return responseData;
    }

    @RequestMapping(value = "/phone/{phone}", method = { RequestMethod.GET })
    public @ResponseBody
    ResponseData phone(Massage massage){
        String code = SMSUtils.randomCode();
        System.out.println(massage.getPhone());
        massage.setCode(code);
        massage.setCreateTime(new Date());
        System.out.println(massage.getCode());
        massageService.SavePhoneAndCode(massage.getPhone(),massage.getCode());
        SMSUtils.sendCode(massage.getPhone(),massage.getCode());
        ResponseData responseData = ResponseData.ok();
        return responseData;
    }

    @RequestMapping(value = "/test/{phone}", method = { RequestMethod.GET })
    public @ResponseBody
    ResponseData test(Massage massage){
        UserVerification userVerification = userVerificationService.selectByPhone(massage.getPhone());

        userVerification = this.userVerificationService.login(userVerification);
        System.out.println("token:" + userVerification);
        ResponseData responseData = ResponseData.ok();
        if (userVerification != null) {
            // 生成token
            String token = JWTUtil.generToken("1", "Jersey-Security-Basic", userVerification.getLoginToken());
            // 向浏览器返回token，客户端受到此token后存入cookie中，或者h5的本地存储中
            responseData.putDataValue("token", token);
            System.out.println("用户id"+userVerification.getuId()+"登录校验成功");
            // 以及用户信息
            User user = this.userService.selectByPrimaryKey(userVerification.getuId());
            try {
                System.out.println("用户登录:"+user.getuName());
            }catch (NullPointerException e){
                System.out.println("登录成功，但是出现异常，未找到登录的用户");
            }

            responseData.putDataValue("user", user);

            // 师生信息
            Person person = this.personService.selectByuId(userVerification.getuId());
            responseData.putDataValue("person", person);

            // 以及用户账号密码信息
            responseData.putDataValue("userVerification", userVerification);
        } else {
            // 用户或者密码错误
            responseData = ResponseData.customerError();
        }
        return responseData;
    }


}
